.TH std::swap(std::pair) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::swap(std::pair) \- std::swap(std::pair)

.SH Synopsis
   Defined in header <utility>
   template< class T1, class T2 >
                                                                          \fI(since C++11)\fP
   void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y )                  \fI(until C++20)\fP

       noexcept(/* see below */);
   template< class T1, class T2 >

   constexpr void swap( std::pair<T1,T2>& x, std::pair<T1,T2>& y          \fI(since C++20)\fP
   )                                                              \fB(1)\fP

       noexcept(/* see below */);
   template< class T1, class T2 >

   constexpr void swap( const std::pair<T1,T2>& x, const              \fB(2)\fP (since C++23)
   std::pair<T1,T2>& y )

       noexcept(/* see below */);

   Swaps the contents of x and y. Equivalent to x.swap(y).

   1) This overload participates in overload resolution only if
   std::is_swappable_v<first_type> && std::is_swappable_v<second_type> is
   true.                                                                  \fI(since C++17)\fP
   2) This overload participates in overload resolution only if
   std::is_swappable_v<const first_type> && std::is_swappable_v<const
   second_type> is true.

.SH Parameters

   x, y - pairs whose contents to swap

.SH Return value

   \fI(none)\fP

.SH Exceptions

   noexcept specification:
   noexcept(noexcept(x.swap(y)))

.SH Example


// Run this code

 #include <iostream>
 #include <utility>

 int main()
 {
     auto p1 = std::make_pair(10, 3.14);
     auto p2 = std::pair(12, 1.23); // CTAD, since C++17

     auto print_p1_p2 = [&](auto msg) {
         std::cout << msg
                   << "p1 = {" << std::get<0>(p1)
                   << ", "     << std::get<1>(p1) << "}, "
                   << "p2 = {" << std::get<0>(p2)
                   << ", "     << std::get<1>(p2) << "}\\n";
     };

     print_p1_p2("Before p1.swap(p2): ");
     p1.swap(p2);
     print_p1_p2("After  p1.swap(p2): ");
     std::swap(p1, p2);
     print_p1_p2("After swap(p1, p2): ");
 }

.SH Output:

 Before p1.swap(p2): p1 = {10, 3.14}, p2 = {12, 1.23}
 After  p1.swap(p2): p1 = {12, 1.23}, p2 = {10, 3.14}
 After swap(p1, p2): p1 = {10, 3.14}, p2 = {12, 1.23}

.SH See also

   swap                  swaps the values of two objects
                         \fI(function template)\fP
   std::swap(std::tuple) specializes the std::swap algorithm
   \fI(C++11)\fP               \fI(function template)\fP

.SH Category:
     * conditionally noexcept
